<?php
/** [会员管理控制器]
 * @Author: 976123967@qq.com
 * @Date:   2015-01-19 12:23:00
 * @Last Modified by:   happy
 * @Last Modified time: 2015-03-14 18:43:45
 */
class UserAction extends PublicAction{


	public $logic;
	/**
	 * [_initialize 初始化]
	 * @return [type] [description]
	 */
	public function _initialize()
	{
		parent::_initialize();
		$this->logic = D('User','Logic');
	}
	/**
	 * [index 会员列表]
	 * @return [type] [description]
	 */
	public function index()
	{
		// 读取会员列表 1会员 2 普通会员
		$data = $this->logic->get_all(array(2));

		if($data)
		{
			$this->assign('page',$data['page']);
			unset($data['page']);
		}
		$this->assign('data',$data);
		$this->display();
	}


		/**
	 * [add 添加]
	 */
	public function add()
	{
		if(IS_POST)
		{
			if(!$this->logic->add_user())
				$this->error($this->logic->getError());
			$this->success('会员添加成功',U('User/index'));
			die;
		}

		$this->display();
	}


	/**
	 * [edit 编辑]
	 * @return [type] [description]
	 */
	public function edit()
	{
		if(IS_POST)
		{
			if(!$this->logic->update_user())
				$this->error($this->logic->getError());
			$this->success('会员修改成功',U('User/index'));
			die;
		}
		$field = $this->logic->find_one();
		$this->assign('field',$field);
		$this->display();
	}


	/**
	 * [del 删除]
	 * @return [type] [description]
	 */
	public function del()
	{	
		if(!$this->logic->delete_user())
			$this->error($this->logic->getError());
		$this->success('会员删除成功',U('User/index'));
	}

	/**
	 * [operation 批量操作数据]
	 * @return [type] [description]
	 */
	public function operation()
	{
		if(!$msg = $this->logic->operation())
			$this->error($this->logic->getError());
		$this->success($msg,U('User/index'));
	}

	/**
	 * [export 会员导出]
	 * @return [type] [description]
	 */
	public function export()
	{
		$data = D('User')->where(array('role'=>2))->select();
		load('Class.PHPExcel180.PHPExcel',APP_PATH);

   		$objPHPExcel = new PHPExcel(); //创建PHPExcel对象

		//合并excel
		$objPHPExcel->getActiveSheet()->mergeCells('A1:H1');


		

		//设置宽度
		$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
		$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(48);
		$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(48);
		$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(30);
		$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(30);
		$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(30);
		$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(30);
		$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(30);
		
		//设置行高
		$objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(30);
		$objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(20);
		//设置字体样式
		$objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10); //默认字体大小
		$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(16)->setBold(true);
		$objPHPExcel->getActiveSheet()->getStyle('A2:H2')->getFont()->setBold(true); //粗体



		//设置垂直、水平居中
		$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()
			->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER)
			->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objPHPExcel->getActiveSheet()->getStyle('A2:H2')->getAlignment()
			->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER)
			->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);


		//设置边框
		$objPHPExcel->getActiveSheet()->getStyle('A1:H1')->getBorders()->getAllBorders()
			->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objPHPExcel->getActiveSheet()->getStyle('A2:H2')->getBorders()->getAllBorders()
			->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

		//前两行单元格内容
		$objPHPExcel->setActiveSheetIndex(0)
			->setCellValue('A1','会员帐号')
			->setCellValue('A2','用户名')
			->setCellValue('B2','密码')
			->setCellValue('C2','邮箱')
			->setCellValue('D2','昵称')
			->setCellValue('E2','登录时间')
			->setCellValue('F2','登录次数')
			->setCellValue('G2','注册时间')
			->setCellValue('H2','状态');

		//数据行设置
		for($i = 0;$i < count($data);$i++)
		{
			
			// 设置值
			$objPHPExcel->getActiveSheet()->setCellValue('A' . ($i+3), $data[$i]['username']);
			$objPHPExcel->getActiveSheet()->setCellValue('B' . ($i+3), $data[$i]['password']);
			$objPHPExcel->getActiveSheet()->setCellValue('C' . ($i+3), $data[$i]['email']);
			$objPHPExcel->getActiveSheet()->setCellValue('D' . ($i+3), $data[$i]['nickname']);
			$objPHPExcel->getActiveSheet()->setCellValue('E' . ($i+3), $data[$i]['login_time']?date('Y-m-d H:i:s',$data[$i]['login_time']):'');
			$objPHPExcel->getActiveSheet()->setCellValue('F' . ($i+3), $data[$i]['times']);
			$objPHPExcel->getActiveSheet()->setCellValue('G' . ($i+3), $data[$i]['reg_time']?date('Y-m-d H:i:s',$data[$i]['reg_time']):'');
			$objPHPExcel->getActiveSheet()->setCellValue('H' . ($i+3), $data[$i]['lock']?'锁定':'正常');
			
			
			//设置垂直、水平居中
			$objPHPExcel->getActiveSheet()->getStyle('A' . ($i+3).':H'.($i+3))->getAlignment()
				->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER)
				->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
				
			$objPHPExcel->getActiveSheet()->getRowDimension($i+3)->setRowHeight(16);//行高
				
			//设置边框
			$objPHPExcel->getActiveSheet()->getStyle('A' . ($i+3).':H'.($i+3))->getBorders()->getAllBorders()
				->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		}

		//sheet命名
		$objPHPExcel->getActiveSheet()->setTitle('user表');

		//默认打开的sheet
		$objPHPExcel->setActiveSheetIndex(0);

		//excel头参数
		header("Content-Type:application/vnd.ms-execl");
		header('Content-Disposition:attachment;filename=user表查询结果('.date('YmdHis').').xls');//日期文件名后缀
		header('Cache-Control:max-age=0');
		
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); //excel2007为xlsx
		$objWriter->save('php://output');
	}


}